package com.xy.tianyu.module.video.adapter

import android.content.Intent
import android.view.View
import android.widget.ImageView
import androidx.core.content.ContextCompat
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.rxlife.coroutine.RxLifeScope
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper
import com.shuyu.gsyvideoplayer.video.base.GSYVideoView
import com.xy.tianyu.R
import com.xy.tianyu.constant.Constant
import com.xy.tianyu.module.video.activity.VideoRelateActivity
import com.xy.tianyu.module.video.bean.Re
import com.xy.tianyu.module.video.bean.VideoPlayBean
import com.xy.tianyu.module.video.bean.VideoPlayLinkBean
import com.xy.tianyu.utils.DoubleUtils
import com.xy.tianyu.utils.GlideUtils
import com.xy.tianyu.utils.UniqueUtils
import kotlinx.android.synthetic.main.item_video_list.view.*
import kotlinx.coroutines.cancel
import rxhttp.RxHttp
import rxhttp.toClass
import java.text.SimpleDateFormat

/**
 * @time 2020/9/25 14:25
 * @class 描述:
 */
class VideoListAdapter(layoutResId: Int, data: MutableList<Re>?, var isRelateVideo:Boolean,var gsyHelper: GSYVideoHelper, var gsyBuilder:GSYVideoHelper.GSYVideoHelperBuilder) :
    BaseQuickAdapter<Re, BaseViewHolder>(layoutResId, data) {
    private var timeFormat = SimpleDateFormat("mm:ss")
    companion object{
        const val TAG = "VideoListAdapter"
    }
    override fun convert(holder: BaseViewHolder, item: Re) {
        holder.itemView.tv_video_title.text = item.t
        val mIvCover = ImageView(context)
        mIvCover.scaleType = ImageView.ScaleType.CENTER_CROP
        var position = holder.layoutPosition
        Glide.with(context).load(item.i).apply(GlideUtils.getVideoRequestOptions()).into(mIvCover)
        Glide.with(context).load(item.fromicon).apply(GlideUtils.getRoundRequestOptions()).into(holder.itemView.iv_video_author_head)
        holder.itemView.tv_video_author.text = item.f
        holder.itemView.tv_video_duration.text = timeFormat.format(item.duration.toDouble().toInt()*1000).toString()
        val playCnt = item.playcnt
        if (playCnt>10000){
            holder.itemView.tv_video_play_num.text = String.format(context.getString(R.string.ty_video_wan_play_cnt),DoubleUtils.div(playCnt.toDouble(),10000.toDouble(),1))
        }else{
            holder.itemView.tv_video_play_num.text = String.format(context.getString(R.string.ty_video_play_cnt),playCnt)
        }
        gsyHelper.addVideoPlayer(position,mIvCover, TAG,holder.itemView.fr_video_container,holder.itemView.iv_video_play)
        var videoUrl = item.videoUrl
        val playing = gsyHelper.gsyVideoPlayer.currentPlayer.isInPlayingState
        if (playing){
            holder.itemView.tv_video_title.visibility = View.GONE
            gsyHelper.gsyVideoPlayer.titleTextView.visibility = View.VISIBLE
        }else{
            holder.itemView.tv_video_title.visibility = View.VISIBLE
            gsyHelper.gsyVideoPlayer.titleTextView.visibility = View.GONE
        }
        holder.itemView.iv_video_play.setOnClickListener {
         RxLifeScope().launch({
             //视频播放地址要经过两轮解析
             val videoPlayBean = videoUrl?.let { it1 -> getPlayUrl(it1) }
             val playLinkUrl = videoPlayBean?.data!!.playLink
             val videoPlayLinkBean = playLinkUrl?.let { getPlayLinkUrl(it) }
             //正式播放地址
             val url = videoPlayLinkBean?.data?.url
             notifyDataSetChanged()
             gsyHelper.setPlayPositionAndTag(position, TAG)
             gsyBuilder.setVideoTitle(item.t)
                 .setUrl(url)
             gsyHelper.startPlay()
             holder.itemView.tv_video_title.visibility = View.GONE
             gsyHelper.gsyVideoPlayer.titleTextView.visibility = View.VISIBLE
             cancel()
         },{},{},{})
        }

        if (isRelateVideo){
            holder.itemView.cst_video_info.setBackgroundColor(ContextCompat.getColor(context,R.color.black))
            holder.itemView.tv_video_play_num.setTextColor(ContextCompat.getColor(context,R.color.white))
            holder.itemView.tv_video_author.setTextColor(ContextCompat.getColor(context,R.color.white))
        }else{
            holder.itemView.cst_video_info.setBackgroundColor(ContextCompat.getColor(context,R.color.white))
            holder.itemView.tv_video_play_num.setTextColor(ContextCompat.getColor(context,R.color.black))
            holder.itemView.tv_video_author.setTextColor(ContextCompat.getColor(context,R.color.black))
            holder.itemView.cst_video_info.setOnClickListener {
                val intent = Intent(context,VideoRelateActivity::class.java)
                intent.putExtra(VideoRelateActivity.INTENT_RELATE_VIDEO,item)
                context.startActivity(intent)
            }
        }
    }

    private suspend fun getPlayUrl(videoUrl:String):VideoPlayBean{
        return RxHttp.get(videoUrl)
            .add(Constant.VIDEO_ACCESS_TOKEN,UniqueUtils.getVideoAccessToken())
            .toClass<VideoPlayBean>()
            .await()
    }


    private suspend fun getPlayLinkUrl(url:String): VideoPlayLinkBean {
        return RxHttp.get(url)
            .add(Constant.VIDEO_ACCESS_TOKEN,UniqueUtils.getVideoAccessToken())
            .toClass<VideoPlayLinkBean>()
            .await()
    }
}